﻿using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace PMIS.Common.Converter
{
    public class StringToDateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string str)
            {
                DateTime date;
                if (DateTime.TryParseExact(str, "yyyy-MM-dd", culture, DateTimeStyles.None, out date))
                {
                    return date;
                }
            }
            return DependencyProperty.UnsetValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DateTime date)
            {
                return date.ToString("yyyy-MM-dd", culture);
            }
            return null;
        }
    }
}
